package com.sync.utils.framework.handler;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.sync.utils.framework.entity.BaseEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;

/**
 * TODO
 *
 * @Author YWF
 * @Date 2023/7/29 22:53
 */
@Slf4j
public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        if (ObjectUtil.isNotNull(metaObject.getOriginalObject()) && metaObject.getOriginalObject() instanceof BaseEntity) {
            BaseEntity baseEntity = ((BaseEntity) metaObject.getOriginalObject());
            Date currentDate = ObjectUtil.isNotNull(baseEntity.getCreateTime())?baseEntity.getCreateTime():new Date();
            baseEntity.setCreateTime(currentDate);
            baseEntity.setUpdateTime(currentDate);
            //  TODO 还可以获取用户信息，放到userId
            log.info("BaseEntity插入数据的时间信息填充成功");
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if (ObjectUtil.isNotNull(metaObject.getOriginalObject()) && metaObject.getOriginalObject() instanceof BaseEntity) {
            BaseEntity baseEntity = ((BaseEntity) metaObject.getOriginalObject());
            baseEntity.setUpdateTime(new Date());
            //  TODO 还可以获取用户信息，放到userId
            log.info("BaseEntity更新数据的时间信息填充成功");
        }
    }
}
